

import { ref, reactive } from 'vue'
import { editPasswordApi } from '@/api/login.js' 
import { ElMessage } from 'element-plus'
export function useEditPassword() {
  const ruleFormRef = ref(null)
  const dialogFormVisible = ref(false)
  const ruleForm = reactive({
    oldpassword: '',
    password: '',
    repassword: ''
  })
  const rules = reactive({
    oldpassword: [
      { required: true, message: '请输入旧密码', trigger: 'blur' },
      { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur', }
    ],
    password: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur', }
    ],
    repassword: [
      { required: true, message: '请输入确认密码', trigger: 'blur' },
      { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur', }
    ],
  })
  const handleClose = () => {
    ruleFormRef.value.resetFields()
  }
  const handleOk = () => {
    ruleFormRef.value.validate(async valid => {
      if (!valid) return
      const res = await editPasswordApi(ruleForm)
      if (res.msg && res.msg !== 'ok') {
        ElMessage.error(res.msg)
        return
      }
      dialogFormVisible.value = false
      ElMessage.success({
        message: '密码修改成功',
        type: 'success',
      })
    })
  }

  return {
    ruleFormRef,
    dialogFormVisible,
    ruleForm,
    rules,
    handleClose,
    handleOk
  }
}
